<?php

namespace app\teacher\controller;
use app\common\model\UserBadge;
use EasyWeChat\Factory;
use think\Db;
use think\Controller;
use app\admin\model\Badge as BadgeModel;
class Badge extends Base
{

    protected $config = [
        'app_id' => 'wx3cf0f39249eb0exx',
        'secret' => 'f1c242f4f28f735d4687abb469072axx',
        // 下面为可选项
        // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
        'response_type' => 'array',
        'log' => [
            'level' => 'debug',
            'file' => __DIR__.'/wechat.log',
        ],
    ];

    //小程序码
    public function get_qr_code(){
        $app = Factory::miniProgram($this->config);
        $url = $this->request->post('url');
        $response = $app->app_code->getQrCode($url);

        // $response 成功时为 EasyWeChat\Kernel\Http\StreamResponse 实例，失败为数组或你指定的 API 返回类型
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $filename = $response->saveAs('/pages/user/login', 'appcode.png');
        }
        return $filename;
    }

    public function lists()
    {
        $list = BadgeModel::select();
        foreach($list as $k=>$v) {
            $v['level'] = 0;
            if ($v['id'] == 2) {
                for($i = 1; $i <= 5; $i++) {
                    $v['level'] = $i;
                    $list[] = $v;
                }
                unset($list[$k]);
            }
        }
        $this->success('成功', array_values($list));
    }

    public function user_badge()
    {
        $id = $this->request->get('id');
        $level = $this->request->get('level', 1);
        if (!$id) $this->error('参数错误');
        $page = $this->request->get('page', 1);
        $limit = $this->request->get('limit', 10);
        $where = ['id'=>$id];
        if ($id == 2) $where['level'] = $level;
        $list = UserBadge::where($where)->with('student')->page($page, $limit)->select();
        $count = UserBadge::where($where)->count();
        $this->success('成功', ['list'=>$list, 'count'=>$count]);
    }

    public function get_jiaji(){
        $data = Db::name('exam_papers_gl')->where(['type'=>1,'is_huizhang'=>0])->select();
        $now_time = time();
        foreach ($data as $key => $value){
            if($value['end_time'] < $now_time) {
                $res['classes_id'] = $value['classes_id'];
                $res['profile_id'] = $value['profile_id'];
                $res['type'] = 3;
                $res['createtime'] = $now_time;
                $res['updatetime'] = $now_time;
                $count = count(explode(',', $value['question_ids']));
                //当前班级总人数
                $student_count = Db::name('classes_student')->where(['classes_id' => $value['classes_id']])->count();
                //前5%人数
                $bfb = Db::name('badge')->where(['id'=>3])->value('rule_num');
                $num = ceil(($bfb / 100) * $student_count);
                //调用 buildSql 方法后不会进行实际的查询操作，而只是生成该次查询的 SQL 语句
                $subQuery = Db::name('exam_user_achievement')
                    ->field('id,user_id,score,wrong,answer_time')
                    ->order('answer_time desc')
                    ->where(['user_papers_id' => $value['id'], 'score' => $count])
                    ->buildSql();
                //然后使用子查询构造新的查询
                $list = Db::table($subQuery . ' a')
                    ->field('user_id,max(score) as score1,wrong,min(answer_time) as answer_time1')
                    ->group('user_id')
                    ->select();
                if ($list) {
                    $lists = arr_sort($list,$num);
                    foreach ($lists as $key1 => $value1) {
                        $res['user_id'] = $value1['user_id'];
                        Db::name('user_badge')->insert($res);
                    }
                }
                Db::name('exam_papers_gl')->where(['id' => $value['id']])->update(['is_huizhang' => 1]);
            }
        }
    }

    public function get_ranqi(){
        $data = Db::name('exam_papers_gl')->where(['type'=>2,'is_huizhang'=>0])->select();
        $now_time = time();
        foreach ($data as $key => $value){
            if($value['end_time'] < $now_time) {
                $res['classes_id'] = $value['classes_id'];
                $res['profile_id'] = $value['profile_id'];
                $res['type'] = 4;
                $res['createtime'] = $now_time;
                $res['updatetime'] = $now_time;
                $count = count(explode(',', $value['question_ids']));
                //当前班级总人数
                $student_count = Db::name('classes_student')->where(['classes_id' => $value['classes_id']])->count();
                //前5%人数
                $bfb = Db::name('badge')->where(['id'=>4])->value('rule_num');
                $num = ceil(($bfb / 100) * $student_count);
                //调用 buildSql 方法后不会进行实际的查询操作，而只是生成该次查询的 SQL 语句
                $subQuery = Db::name('exam_user_achievement')
                    ->field('id,user_id,score,wrong,answer_time')
                    ->order('answer_time desc')
                    ->where(['user_papers_id' => $value['id'], 'score' => $count])
                    ->buildSql();
                //然后使用子查询构造新的查询
                $list = Db::table($subQuery . ' a')
                    ->field('user_id,max(score) as score1,wrong,min(answer_time) as answer_time1')
                    ->group('user_id')
                    ->select();
                if ($list) {
                    $lists = arr_sort($list,$num);
                    foreach ($lists as $key1 => $value1) {
                        $res['user_id'] = $value1['user_id'];
                        Db::name('user_badge')->insert($res);
                    }
                }
                Db::name('exam_papers_gl')->where(['id' => $value['id']])->update(['is_huizhang' => 1]);
            }
        }
    }
}